<template>
  <div class="addHouse">
    <h3 class="title">添加房产信息</h3>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="150px"
      class="demo-ruleForm"
    >
      <el-form-item label="所属人	" prop="master">
        <el-select v-model="ruleForm.master" 
          clearable
          filterable
          placeholder="请选择">
          <el-option
            v-for="item in users"	
            :key="item.userName"			
            :label="item.userName"		
            :value="item.id"/>	
          </el-select>
      </el-form-item>
      <el-form-item label="房产地址" prop="houseAddress">
        <el-input v-model="ruleForm.houseAddress"></el-input>
      </el-form-item>
      <el-form-item label="成交价" prop="price">
        <el-input v-model="ruleForm.price"></el-input>
      </el-form-item>
      <el-form-item label="成交日期" prop="fixtureTime">
        <el-date-picker
          v-model="ruleForm.fixtureTime"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="修改成交日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="产权" prop="propertyRight">
        <el-input v-model="ruleForm.propertyRight"></el-input>
      </el-form-item>
      <el-form-item label="合同附件" prop="dataId">
        <el-upload
          class="upload-demo"
          :action="action"
          :on-remove="handleRemove"
          :on-change="fileChange"
          :data="data"
          multiple
          :limit="3"
          :file-list="fileList"
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :plain="true" @click="submitForm('ruleForm')"
          >保存</el-button
        >
        <el-button type="info" :plain="true" @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "../request/axios";
import userRequest from "../request/user";
export default {
  data() {
    var master = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写所属人名称"));
      } else {
        return callback();
      }
    };
    var propertyRight = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写产权"));
      } else {
        return callback();
      }
    };
    var fixtureTime = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请选择成交日期"));
      } else {
        return callback();
      }
    };
    var houseAddress = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写成交地址名称"));
      } else {
        return callback();
      }
    };
    var price = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填成交价"));
      } else {
        return callback();
      }
    };

    return {
      users: [],
      fileList: [],

      data: {
        dataId: Math.random().toString(36).substr(3, 6) + Date.now().toString(),
      },
      action: axios.defaults.baseURL + "centre/centre/file/upload",

      ruleForm: {
        houseAddress: "",
        fixtureTime: "",
        master: "",
        price: "",
        propertyRight: "",
      },
      rules: {
        master: [{ validator: master, trigger: "blur" }],
        price: [{ validator: price, trigger: "blur" }],
        houseAddress: [{ validator: houseAddress, trigger: "blur" }],
        fixtureTime: [{ validator: fixtureTime, trigger: "blur" }],
        propertyRight: [{ validator: propertyRight, trigger: "blur" }],
      },
    };
  },

  mounted(){
    this.getUserList();
  },

  methods: {

    getUserList(){
      this.users = []
      userRequest.getUsers().then((res) => {
          if (res.data.code === 200) {
            const result = res.data.result;
            this.users = result;
            console.log(this.users);
            setTimeout(() => {
              this.loading = false;
            }, 500);
          }
        });
    },

    fileChange(fileList) {
      if (fileList.length > 0) {
        this.fileList = [fileList[fileList.length - 1]];
      }
    },

    handleRemove() {
      userRequest.deleteFile(this.data.dataId)
      console.log("success");
    },  

    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          userRequest
            .addHouse({
              dataId: this.data.dataId,
              master: this.ruleForm.master,
              price: this.ruleForm.price,
              fixtureTime: this.ruleForm.fixtureTime,
              propertyRight: this.ruleForm.propertyRight,
              houseAddress: this.ruleForm.houseAddress,
            })
            .then((res) => {
              if (res.data.success == true) {
                this.$message.success("添加成功!");
                this.resetForm();
              } else {
                this.$message.error(res.data.message);
              }
            });
        } else {
          this.$message.error("请检查信息是否完整！");
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.ruleForm.resetFields();
    }
  },

  created() {},
};
</script>

<style lang="scss" scoped>
.addHouse {
  overflow: hidden;

  ::v-deep .title {
    text-align: center;
    font-size: 20px;
  }
  ::v-deep .demo-ruleForm {
    width: 500px;
    margin: 30px auto;
  }
  ::v-deep .el-input__inner {
    height: 45px;
    font-size: 16px;
  }
  ::v-deep .el-form-item__label {
    font-size: 16px;
    position: relative;
    top: 10px;
  }
  ::v-deep .el-form-item__content {
    margin-top: 10px;
    height: 50px;
    display: flex;
  }
  ::v-deep .el-form-item__error {
    font-size: 16px;
  }

  ::v-deep .el-select-dropdown__item {
    display: flex;
    justify-content: center;
  }
  ::v-deep .el-radio-group {
    width: 100px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 11px;
  }
}
</style>